package com.yanggu.scala.chapater07.instance

object InstanceDemo01 {

  def main(args: Array[String]): Unit = {
    //1. scala提供了classOf[类名]。返回String.class。scala中没有String.class
    //2. scala提供了obj.isInstanceOf[T]如同java的 obj instanceof T 判断obj是不是T类型
    //3. scala提供了obj.asInstanceof[T]就如同java的强制类型转换(T) obj，将obj类型强制转换成T类型。
    val clzz = classOf[String]
    println(clzz)
    val student = new Student
    //编译器推荐使用模式匹配, 而不是使用if else的方式
    if (student.isInstanceOf[Person]) {
      val person = student.asInstanceOf[Person]
      person.printName()
    }
    //student match {
    //  case person: Person =>
    //    person.printName()
    //  case _ =>
    //}
  }

}
class Person {

  var name : String = "jack"

  private val sal : Double = 9999.9

  protected  var age = 10

  var job : String = "大数据工程师"

  def showInfo(): Unit = {
    println("name = " + name + ", sal = " + sal + ", job = " + job)
  }

  def printName(): Unit = {
    println(name)
  }

}

class Student extends Person {

  def study(): Unit = {
    println("name = " + name + ", job = " + job)
    //    子类无法访问父类的私有属性，因为sal()方法被私有了
    //    println(sal)
  }

  override def printName(): Unit = {
    println("子类重写父类的方法")
    println(name)
  }

}